//index.js
//获取应用实例
const app = getApp();
const array = require("../../utils/data.js");
const util = require("../../utils/util.js");
Page({
  data: {
    url:'https://wx.woqutech.cn/',
    loadRec:false,
    array1: array.data1,
    array2: array.data2,
    nowDate: util.formatTime(new Date).replace(new RegExp("/", "g"), '-'),
    index1: 13,
    index2: 6,
    test:1,
    date: util.formatTime(new Date).replace(new RegExp("/", "g"), '-'),
    calender: "",
    calenderData: '',
    isFirst: app.globalData.isFirst,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  btnClick: function() {
    const _that = this;
    showData2(app.globalData.sessionId);
    function showData2(sessionId){
      wx.request({
        url: _that.data.url+'Mobile/Menstruation/setConfig',
        method: "POST", 
        data: { cycle: _that.data.array1[_that.data.index1], days: _that.data.array2[_that.data.index2], sessionId: sessionId},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {
          app.globalData.isFirst = false;
          app.globalData.isFirst2 = false;
          _that.setData({
            calender: _that.data.url + "calendar/index.html?cycles=" + _that.data.array1[_that.data.index1] + '&days=' + _that.data.array2[_that.data.index2] + '&date=' + _that.data.date + '&ifInit=0&sessionId=' + sessionId,
            isFirst: app.globalData.isFirst,
          });
          wx.showToast({
            title: '设置成功',
            icon: 'success',
            duration: 2000
          });
        },
        fail: function () {
          app.globalData.isFirst = true
        }
      })
      
      app.globalData.setting.cycle = _that.data.array1[_that.data.index1];
      app.globalData.setting.days = _that.data.array2[_that.data.index2];
      app.globalData.beginDate     = _that.data.date;

      console.log(app.globalData.setting)

    };
  },
  onLoad: function () {
    const _that = this;
    app.globalData.cb = function (sessionId) {   //取sessionId回调函数
      
      wx.showLoading({
        title: '请稍等...',
      });

      showData(sessionId);
      _that.setData({
        loadRec:true
      });
    };

    if (this.data.calender == '') {
      _that.setData({
        loadRec: true,
        calender: app.globalData.Url.replace(/ifInit=0/, "ifInit=2")
      });
    };

  
    function showData(sessionId){
      if (!app.globalData.isFirst){
        _that.setData({
          calender: "https://wx.woqutech.cn/calendar/index.html?cycles=" + app.globalData.setting.cycle + "&days=" + app.globalData.setting.days + "&date=" + app.globalData.beginDate + "&ifInit=2&sessionId=" + sessionId,
          isFirst: app.globalData.isFirst,
        });
        app.globalData.Url = _that.data.calender;
      };
      wx.hideLoading()
    };

  },
  bindPickerChange1: function (e) {
    this.setData({
      index1: e.detail.value,
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      index2: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  onShow:function(){
    var that =this;
    this.setData({
      isFirst: app.globalData.isFirst
    });
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
})

